package tx.bj12320.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.hibernate.Hibernate;

import tx.bj12320.model.TszbProcessInfo;
import tx.bj12320.model.UserInfo;
import tx.bj12320.model.enums.BooleanEnum;
import tx.bj12320.model.enums.InfoTypeEnum;
import tx.bj12320.service.InfoissueManager;
import tx.bj12320.service.ResourceManager;
import tx.bj12320.service.TszbProcessInfoManager;
import tx.bj12320.service.UserInfoManager;
import tx.bj12320.util.SpringSecurityUtils;
import tx.bj12320.util.WebUtils;
import tx.bj12320.vo.query.InfoissueQuery;
import tx.bj12320.vo.query.ResourceQuery;
import tx.bj12320.vo.query.TszbTask;



import javacommon.base.BaseStruts2Action;

import cn.org.rapid_framework.page.Page;
import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class PortalAction extends BaseStruts2Action implements Preparable,ModelDriven{

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		return null;
	}
	private UserInfoManager userInfoManager;
	public void setUserInfoManager(UserInfoManager manager) {
		this.userInfoManager = manager;
	}

	private TaskService taskService;
	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	private InfoissueManager infoissueManager;
	public void setInfoissueManager(InfoissueManager manager) {
		this.infoissueManager = manager;
	}

	private ResourceManager resourceManager;
	public void setResourceManager(ResourceManager manager) {
		this.resourceManager = manager;
	}

	/**
	 * 进入主页
	 * @return
	 */
	public String index(){
		String username=SpringSecurityUtils.getCurrentUserName();
		if(!SpringSecurityUtils.isAnonymousUser()){

				UserInfo u=userInfoManager.getByUsername(username);
				Hibernate.initialize(u.getOrgInfo());
				WebUtils.setUserInSession(u);

		}
		return "/index.jsp";
	}

	private String password;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	/**
	 * 修改密码
	 * @return
	 */
	public String changePassword(){
		if(!SpringSecurityUtils.isAnonymousUser()){
			UserInfo u=userInfoManager.getById(WebUtils.getUserInSession().getUserInfoId());
			u.setPassword(password);
			userInfoManager.saveOrUpdate(u);
		}
		Flash.current().success(UPDATE_SUCCESS);
		return "!/pages/Portal/main.do";
	}


	/**
	 * 进入main页面
	 * @return
	 */
	public String main(){

//		initMain_usertasks();

		initMain_infoissue_xinwen();

		initMain_infoissue_tongzhi();

		initMain_infoissue_gzdt();

		initMain_resource_url();

		return "/main.jsp";
	}
	/**
	 * 初始化常用网址
	 */
	private void initMain_resource_url() {
		ResourceQuery query = newQuery(ResourceQuery.class,
				ResourceAction.DEFAULT_SORT_COLUMNS);
		query.setResourceType(new Long(11));// 常用网址类型为11
		query.setPageSize(5);
		Page page = resourceManager.findPage(query);
		getRequest().setAttribute("resource_url", page.getResult());
	}

	/**
	 * 初始化新闻
	 */
	private void initMain_infoissue_xinwen() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				InfoissueAction.DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(0));
		query.setIsInfoToPublish(BooleanEnum.FALSE.getCode());
		query.setInfoType(InfoTypeEnum.ZCFG.getCode());
		query.setPageSize(5);
		Page page = infoissueManager.findPage(query);
		getRequest().setAttribute("infoissue_xinwen", page.getResult());
	}

	/**
	 * 初始化通知
	 */
	private void initMain_infoissue_tongzhi() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				InfoissueAction.DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(0));
		query.setIsInfoToPublish(BooleanEnum.FALSE.getCode());
		query.setInfoType(InfoTypeEnum.TZTG.getCode());
		query.setPageSize(5);
		Page page = infoissueManager.findPage(query);
		getRequest().setAttribute("infoissue_tongzhi", page.getResult());
	}

	/**
	 * 初始化工作动态
	 */
	private void initMain_infoissue_gzdt() {
		InfoissueQuery query = newQuery(InfoissueQuery.class,
				InfoissueAction.DEFAULT_SORT_COLUMNS);
		query.setIsMail(new Long(0));
		query.setIsInfoToPublish(BooleanEnum.FALSE.getCode());
		query.setInfoType(InfoTypeEnum.YJDT.getCode());
		query.setPageSize(5);
		Page page = infoissueManager.findPage(query);
		getRequest().setAttribute("infoissue_gzdt", page.getResult());
	}
	private TszbProcessInfoManager tszbProcessInfoManager;
	public void setTszbProcessInfoManager(TszbProcessInfoManager manager) {
		this.tszbProcessInfoManager = manager;
	}
	private void initMain_usertasks() {
		List<Task> tasks = taskService.createTaskQuery().taskCandidateUser(WebUtils.getUserInSession().getUserInfoId()+"").processDefinitionKey("Tousu_zhuanban_process").orderByTaskCreateTime().asc().listPage(0,5);

		List<TszbTask> ltts=new ArrayList<TszbTask>(tasks.size());
		for (Task task : tasks) {
			TszbTask tt=new TszbTask();
			tt.setTask(task);
			TszbProcessInfo tpi=tszbProcessInfoManager.getByProcInstId(task.getProcessInstanceId());
			tt.setOrgInfoId(tpi.getOrgInfoId());
			tt.setOrgName(tpi.getOrgInfo().getOrgName());
			tt.setTszbProcessInfoId(tpi.getTszbProcessInfoId());
			tt.setWorkOrderType(tpi.getWorkOrderType());
			UserInfo ui=tpi.getLastOperateUserInfo();
			if(ui!=null){
				tt.setLastOperateUserInfoRealName(ui.getRealName());
			}

			tt.setIsEmergency(tpi.getIsEmergency());
//			Map<String,Object> vars=taskService.getVariables(task.getId());
//			if(vars.size()>0){
//				tt.setOrgInfoId((Long)vars.get("orgInfoId"));
//				tt.setOrgName((String)vars.get("orgName"));
//				tt.setTszbProcessInfoId((Long)vars.get("tszbProcessInfoId"));
//				tt.setWorkOrderType((String)vars.get("workOrderType"));
//			}

			ltts.add(tt);
		}

		getRequest().setAttribute("tasks", ltts);
	}






}
